package com.wythe.mall.activity;

import android.content.Intent;
import android.location.Address;
import android.os.Bundle;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatTextView;
import android.support.v7.widget.SwitchCompat;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.TextView;
import android.widget.Toast;

import com.wythe.mall.BaseApplication;
import com.wythe.mall.R;
import com.wythe.mall.base.BaseActivity;
import com.wythe.mall.intf.OnRequestDataListener;
import com.wythe.mall.model.AddressBean;
import com.wythe.mall.utils.ApiOkHttp;
import com.wythe.mall.utils.GotoActivity;
import com.wythe.mall.utils.SharePreferenceUtils;
import com.wythe.mall.utils.StringUtils;
import com.wythe.mall.utils.UserManager;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Objects;

import top.yokey.base.base.BaseToast;
import top.yokey.base.util.TextUtil;

public class AddressAddActivity extends BaseActivity implements View.OnClickListener {

    private Toolbar mainToolbar;
    private AppCompatEditText nameEditText;
    private AppCompatEditText mobileEditText;
    private AppCompatEditText areaEditText;
    private AppCompatEditText addressEditText;
    private SwitchCompat defaultSwitch;
    private AppCompatTextView saveTextView;

    private String areaNameString;
    private String cityNameString;
    private String provinceNameString;
    private String areaInfo;
    private AddressBean addressBean;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mine_address_add);
        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            addressBean = (AddressBean) bundle.get("address");
            provinceNameString = addressBean.getProvince();
            cityNameString = addressBean.getCity();
            areaNameString = addressBean.getDistrice();
        }
        initView();
        initData();
    }

    public void initView() {
        findViewById(R.id.title_left_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        ((TextView) findViewById(R.id.titleText)).setText("添加地址");
        nameEditText = (AppCompatEditText) findViewById(R.id.nameEditText);
        mobileEditText = (AppCompatEditText) findViewById(R.id.mobileEditText);
        areaEditText = (AppCompatEditText) findViewById(R.id.areaEditText);
        addressEditText = (AppCompatEditText) findViewById(R.id.addressEditText);
        defaultSwitch = (SwitchCompat) findViewById(R.id.defaultSwitch);
        saveTextView = (AppCompatTextView) findViewById(R.id.saveTextView);
    }

    public void initData() {
        if (addressBean != null) {
            nameEditText.setText(addressBean.getName());
            mobileEditText.setText(addressBean.getMobile());
            areaEditText.setText(addressBean.getProvince() + addressBean.getCity() + addressBean.getDistrice());
            addressEditText.setText(addressBean.getAddress());
            defaultSwitch.setChecked(addressBean.getStatus() == 1 ? true : false);
        }
        areaEditText.setOnClickListener(this);
        saveTextView.setOnClickListener(this);
    }

    public void initEven() {
        addAddress();
    }

    public void hideKeyboard() {
        InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
        if (inputMethodManager != null && inputMethodManager.isActive()) {
            inputMethodManager.hideSoftInputFromWindow(this.getWindow().peekDecorView().getWindowToken(), 0);
        }
    }

    //自定义方法

    private void addAddress() {
        if (!UserManager.getInstance().isLogin()) {
            GotoActivity.gotoActiviy(this.getActivity(), LoginActivity.class);
            SharePreferenceUtils.removeString(this.getActivity(), "KEY_TOKEN");
            return;
        }

        hideKeyboard();

        String name = nameEditText.getText().toString();
        String phone = mobileEditText.getText().toString();
        String address = addressEditText.getText().toString();
        String areaAddress = areaEditText.getText().toString();
        String isDefault = defaultSwitch.isChecked() ? "1" : "2";

        if (TextUtils.isEmpty(name) || TextUtils.isEmpty(phone) || TextUtils.isEmpty(address)) {
            Toast.makeText(this, "请输入完整的信息！", Toast.LENGTH_SHORT).show();
            return;
        }

        if (!StringUtils.isMobileNO(phone)) {
            Toast.makeText(this, "手机号码格式不正确！", Toast.LENGTH_SHORT).show();
            return;
        }

        if (TextUtils.isEmpty(areaAddress)) {
            Toast.makeText(this, "请选择区域！", Toast.LENGTH_SHORT).show();
            return;
        }

        saveTextView.setEnabled(false);
        saveTextView.setText("添加中...");

        JSONObject param = new JSONObject();
        try {
            param.put("address", address);
            param.put("province", provinceNameString);
            param.put("city", cityNameString);
            param.put("districe", areaNameString);
            param.put("mobile", phone);
            param.put("name", name);
            param.put("status", isDefault);
            param.put("zip", "1");
            if (addressBean != null) {
                param.put("id",addressBean.getId());
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        ApiOkHttp.addAddress(this, param, new OnRequestDataListener() {
            @Override
            public void requestSuccess(int code, JSONObject data) {
                try {
                    int code1 = data.getInt("code");
                    String msg = data.getString("msg");
                    if (code1 == 200) {
                        Toast.makeText(AddressAddActivity.this, msg, Toast.LENGTH_SHORT).show();
                        finish();
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void requestFailure(int code, String msg) {

            }
        });

    }

    @Override
    public void onActivityResult(int req, int res, Intent intent) {
        super.onActivityResult(req, res, intent);
        if (res == RESULT_OK) {
            if (req == 100) {
                provinceNameString = intent.getStringExtra("province_name");
                cityNameString = intent.getStringExtra("city_name");
                areaNameString = intent.getStringExtra("area_name");
                areaInfo = intent.getStringExtra("area_info");
                areaEditText.setText(areaInfo);
            }
        }
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.areaEditText:
                Intent intent = new Intent(this, AreaActivity.class);
                this.startActivityForResult(intent, 100);
                break;
            case R.id.saveTextView:
                addAddress();
                break;
        }
    }
}
